<?php 
namespace app\admin\controller;

/**
 * 权限分类
 */
class AuthClass extends Base{
	public function index(){
		$page = $this->request->param('page/d',1);
		$limit = $this->request->param('limit/d',20);
		$keyword = $this->request->param('keyword','','trim');

		$where=NULL;
		if( $keyword ){
			$where = $keyword ? " `title` LIKE '%".$keyword."%'" : '1=1';
		}
		$totalCount = db('auth_class')->where($where)-> count();
		$list = db('auth_class')->where($where)->page($page,$limit)->order(['id'=>'asc'])->select();
		$data['code'] = 0;
		$data['data'] = $list;
		$data['count'] = $totalCount;
		$data['msg'] = '请求成功';

		if( $this->request->isAjax() ){
			return $data;
		}else{
			return $this->fetch();
		}
	}
	public function add(){
		if($this->request-> isPost() || $this->request-> isAjax()){
			$add['title'] = $this->request->param('title','','trim');
			$add['status'] = $this->request->param('status','0','trim');
			$add['isDel'] = $this->request->param('isDel','0','trim');
			$add['order_num'] = $this->request->param('order_num','0','trim');

			$res = db('auth_class')->insert($add);
			if( $res != 1 ){
				addAdminLog('添加权限分类，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}else{
				addAdminLog('添加权限分类，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}
		}else{
			return $this->fetch();
		}
	}
	// public function status(){
	// 	if($this->request-> isPost() || $this->request-> isAjax()){
	// 		$upd['id'] = $this->request->param('id','','trim');
	// 		$upd['status'] = $this->request->param('status','0','trim');
	// 		$res = db('auth_class')->update($upd);
	// 		if( $res !== false ){
	// 			$data= ['err'=>0,'msg'=>'操作成功！'];
	// 		   	return $data;
	// 		}else{
	// 			$data= ['err'=>1,'msg'=>'操作失败！'];
	// 		   	return $data;
	// 		}
	// 	}else{
	// 		$data= ['err'=>1,'msg'=>'操作失败！'];
	// 		return $data;
	// 	}
	// }

	public function edit(){
		$id = $this->request->param('id','','trim');
		if($this->request-> isPost() || $this->request-> isAjax()){
			$upd['id'] = $id;
			$upd['title'] = $this->request->param('title','','trim');
			$upd['status'] = $this->request->param('status','0','trim');
			$upd['isDel'] = $this->request->param('isDel','0','trim');
			$upd['order_num'] = $this->request->param('order_num','0','trim');

			$res = db('auth_class')->update($upd);
			if( $res !== false ){
				addAdminLog('编辑权限分类，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}else{
				addAdminLog('编辑权限分类，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}
		}else{
			$info = db('auth_class')->where('id = '.$id)->find();
			$this->assign('info',$info);
			return $this->fetch();
		}
	}

	public function del(){
		$id = $this->request->param('id','','trim');
		if($this->request-> isPost() || $this->request-> isAjax()){
			$res = db('auth_class')->where('id = '.$id)->delete();
			if( $res !== false ){
				addAdminLog('删除权限分类，操作成功');
				$data= ['err'=>0,'msg'=>'操作成功！'];
			   	return $data;
			}else{
				addAdminLog('删除权限分类，操作失败');
				$data= ['err'=>1,'msg'=>'操作失败！'];
			   	return $data;
			}
		}else{
			$data= ['err'=>1,'msg'=>'操作失败！'];
			return $data;
		}
	}
}